package com.example.liwushuo.fragment;

import java.util.ArrayList;
import java.util.List;
import com.example.liwushuo.R;
import com.example.liwushuo.adapter.TabAdapter;
import com.example.liwushuo.bean.Candidate;
import com.example.liwushuo.bean.TabTitle;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeInfo extends Fragment {

	private TabTitle data;
	private List<Candidate> channels = new ArrayList<Candidate>();
	private TabPageIndicator tab;
	private ViewPager viewPager;
	private List<Fragment> list;
	private TabAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		data = (TabTitle) getArguments().getSerializable("data");
		channels.addAll(data.getChannels());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_home_info, null);

		initView(view);

		return view;
	}

	private void initView(View view) {
		tab = (TabPageIndicator) view.findViewById(R.id.home_info_tab);
		viewPager = (ViewPager) view.findViewById(R.id.home_info_viewPager);
		list = new ArrayList<Fragment>();

		for (int i = 0; i < channels.size(); i++) {

			Bundle args;

			if ("精选".equals(channels.get(i).getName())) {
				Selection selection = new Selection();
				args = new Bundle();
				args.putInt("id", channels.get(i).getId());
				selection.setArguments(args);
				list.add(selection);
			} else {
				Gift gift = new Gift();
				args = new Bundle();
				args.putInt("id", channels.get(i).getId());
				gift.setArguments(args);
				list.add(gift);
			}
		}
		adapter = new TabAdapter(getChildFragmentManager(), channels, list);
		viewPager.setAdapter(adapter);
		// 设置可以加载6个页面不被系统销毁
		viewPager.setOffscreenPageLimit(6);
		tab.setViewPager(viewPager);

	}
}
